/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2016        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control modules to the FatFs module with a defined API.       */
/*-----------------------------------------------------------------------*/

#include "diskio.h"		/* FatFs lower layer API */
#include "app_inc.h"



/* Definitions of physical drive number for each drive */
#define DEV_RAM		0	/* Example: Map Ramdisk to physical drive 0 */
#define DEV_MMC		1	/* Example: Map MMC/SD card to physical drive 1 */
#define DEV_USB		2	/* Example: Map USB MSD to physical drive 2 */


SDC_Info_T gAppSDCardInfoStruct;
uint8_t    gAppSDCardTxBuf[SDC_BLOCK_SIZE_BYTE * 2u];
uint8_t    gAppSDCardRxBuf[SDC_BLOCK_SIZE_BYTE * 2u];
DSTATUS    Stat = STA_NOINIT;	/* Physical drive status */

/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
    if (pdrv)
    {
        return STA_NOINIT;		/* Supports only drive 0 */
    }
    
    return Stat;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
	if (0 != pdrv) /* only 0 is support. */
    {
        Stat |= STA_NOINIT;
        return STA_NOINIT;
    }
    
    /* 初始化SD卡访问硬件 */
    BSP_InitLPSPI_SDCard();
    SDC_Install(&gBspSdcSpiHwDrvStruct);
    if ( !SDC_InitCard(&gAppSDCardInfoStruct) )
    {
        Stat |= STA_NODISK;
        return STA_NODISK;
    }
    Stat &= ~STA_NOINIT;
    
    return Stat; /* OK */
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Start sector in LBA */
	UINT count		/* Number of sectors to read */
)
{
    bool bStat;

	if (pdrv || !count) return RES_PARERR;		/* Check parameter */
	if (Stat & STA_NOINIT) return RES_NOTRDY;	/* Check if drive is ready */
	
    if (count == 1)
    {
        bStat = SDC_ReadBlock(sector, buff);
    }
    else
    {
        bStat = (0 == SDC_ReadBlocks(sector, count, buff));
    }
    
    return bStat ? RES_OK : RES_ERROR;	/* Return result */
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber to identify the drive */
	const BYTE *buff,	/* Data to be written */
	DWORD sector,		/* Start sector in LBA */
	UINT count			/* Number of sectors to write */
)
{
    bool bStat;
    
	if (pdrv || !count) return RES_PARERR;		/* Check parameter */
	if (Stat & STA_NOINIT) return RES_NOTRDY;	/* Check drive status */
    
    if (count == 1)
    {
        bStat = SDC_WriteBlock(sector, (uint8_t *)buff);
    }
    else
    {
        bStat = (0 == SDC_WriteBlocks(sector, count, (uint8_t *)buff));
    }
    
    return bStat ? RES_OK : RES_ERROR;	/* Return result */
    
}



/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
    DRESULT res = RES_OK;

	if (pdrv) return RES_PARERR;					/* Check parameter */
	if (Stat & STA_NOINIT) return RES_NOTRDY;	/* Check if drive is ready */
    
    
    switch (cmd)
    {
        case CTRL_SYNC :		/* Wait for end of internal write process of the drive */
            break;
        case GET_SECTOR_COUNT :	/* Get drive capacity in unit of sector (DWORD) */
            break;
        case GET_BLOCK_SIZE :	/* Get erase block size in unit of sector (DWORD) */
            break;
        case CTRL_TRIM :	/* Erase a block of sectors (used when _USE_ERASE == 1) */
            break;
        default:
            res = RES_PARERR;
        
    }
    return res;
}

/* EOF. */

